import * as THREE from "./threejs/three.module.js";

const CUBE_TYPES   = ',Z,L,LQ,DB,'  //长方体类别
const CUBE_TYPE_Z  = 'Z'  //立柱
const CUBE_TYPE_L  = 'L'  //梁
const CUBE_TYPE_LQ = 'LQ' //梁墙
const CUBE_TYPE_DB = 'DB' //梁墙
const CUBE_TYPE_DEF = 'Z' //缺省值

const indices = [
    0, 1, 2, 0, 2, 3,   // 下面
    4, 5, 6, 4, 6, 7,   // 上面
    // 4, 0, 1, 4, 1, 5,
    8, 9, 10, 8, 10, 11,   // 前面
    12, 13, 14, 12, 14, 15,   // 后面
    16, 17, 18, 16, 18, 19,   // 左面
    20, 21, 22, 20, 22, 23    // 右面
]
const uvs = new Float32Array([
    0,0,1,0,1,1,0,1,  // 下面 0,6
    0,0,1,0,1,1,0,1,
    0,0,1,0,1,1,0,1,  // 前面 12,6
    0,0,1,0,1,1,0,1,  // 后面 18,6
    0,0,1,0,1,1,0,1,  // 左面 24,6
    0,0,1,0,1,1,0,1,   // 右面 30,6
]);
const groups = [
    {
        "start": 0,
        "count": 6,
        "materialIndex": 0
    },
    {
        "start": 6,
        "count": 6,
        "materialIndex": 1
    },
    {
        "start": 12,
        "count": 6,
        "materialIndex": 2
    },
    {
        "start": 18,
        "count": 6,
        "materialIndex": 3
    },
    {
        "start": 24,
        "count": 6,
        "materialIndex": 4
    },
    {
        "start": 30,
        "count": 6,
        "materialIndex": 5
    }
];

export const addCube = (hCamera, hScene,cubeOpt, hCubes,hImgs,initialPositions) => {
    //相机不能为空
    if(null==hCamera || typeof(hCamera)=='undefined'){
        return null
    }
    //场景不能为空
    if(null==hScene || typeof(hScene)=='undefined'){
        return null
    }
    //长方体数组
    if(null==hCubes || typeof(hCubes)=='undefined' || !(hCubes instanceof Array)){
        return null
    }
    //图片缓存数组
    if(null==hImgs || typeof(hImgs)=='undefined' || !(hImgs instanceof Array)){
        console.log('addCube 图片缓存数组为空，或者不为数组')
        return null
    }
    //长方体数据信息
    if(null==cubeOpt || typeof(cubeOpt)=='undefined'){
        return null
    }
    //长方体数据
    var data = cubeOpt.data;
    if(null==data || typeof(data)=='undefined'){
        cubeOpt.data = {}
        data = cubeOpt.data;
    }
    //长方体坐标转换
    var desc     = data.name

    //长方体坐标点数不少于8个，多余8个只取前面8个
    var points     = cubeOpt.points;
    if(null==points || typeof(points)=='undefined' || !(points instanceof Array)){
        console.log('创建长方体[',desc,']没有指定顶点')
        return
    }
    var iPointSize = points.length
    if(iPointSize!=4 && iPointSize<8){
        console.log('创建长方体[',desc,']顶点个数[',iPointSize,']不为4个或者8个')
        return
    }
    //将4个顶点转为8个顶点
    if(iPointSize==4){
        //由平面4个点转变为8个点
        points = pointsFrom4To8(points,data,opt,desc)
        cubeOpt.points = points
    }
    
    //创建长方体材质
    var materials = [null,null,null,null,null,null];
    var faceOpts   = cubeOpt.faceOpts
    var faceSize   = 0;
    var iIdx       = 0;
    var faceOpt    = null;
    var faceIdx   ;
    var faceObjects = [];
    if(faceOpts instanceof Array){
        faceSize = faceOpts.length
    }
    //长方体材质贴图与填充颜色
    var bPush = true;
    for(iIdx=0;iIdx<faceSize;iIdx++){
        faceOpt = faceOpts[iIdx];
        if(null==faceOpt || typeof(faceOpt)=='undefined'){
            continue
        }
        createFaceObjects(faceOpt,points,materials,faceObjects,bPush);
    }
    if(faceObjects.length<1){
        createCubeGeometry(points,materials,hCubes)
        return
    }
    bPush = false;
    for(iIdx=0;iIdx<faceSize;iIdx++){
        faceOpt = faceOpts[iIdx];
        if(null==faceOpt || typeof(faceOpt)=='undefined'){
            continue
        }
        createFaceObjects(faceOpt,points,materials,faceObjects,bPush,hImgs);
    } 
    
    //查找图片
    function findImage(hImgs,vUrl){
        console.log('findImage vUrl=',vUrl,hImgs)
        //图片缓存数组
        if(null==hImgs || typeof(hImgs)=='undefined' || !(hImgs instanceof Array)){
            return null
        }
        
        var iSize = hImgs.length
        var iIdx
        var oImg
        var sUrl
        for(iIdx=iSize-1;iIdx>=0;iIdx--){
            oImg = hImgs[iIdx]
            if(null==oImg || typeof(oImg)=='undefined'){
                hImgs.splice(iIdx, 1);
                continue
            }
            sUrl = oImg.url
            console.log('findImage iIdx=',iIdx,sUrl,oImg.image)
            if(sUrl==vUrl){
                return oImg.image;
            }
        }
        return null
    }

    //压入图片
    function pushImage(hImgs,vUrl,image){
        //图片缓存数组
        if(null==hImgs || typeof(hImgs)=='undefined' || !(hImgs instanceof Array)){
            return null
        }
        if(null==vUrl || typeof(vUrl)=='undefined' || typeof(vUrl)!='string' || vUrl==''){
            return null
        }
        if(null==image || typeof(image)=='undefined'){
            return null
        }
        console.log('pushImage vUrl',vUrl)
        hImgs.push({url:vUrl,image:image})      
    }

    //将二维xy坐标转变为三维坐标数组
    function createFaceObjects(faceOpt,points,materials,faceObjects,bPush,hImgs){
        if(null==faceOpt || typeof(faceOpt)=='undefined'){
            return
        }
        if(null==materials || typeof(materials)=='undefined' || !(materials instanceof Array)){
            return
        }
        if(null==faceObjects || typeof(faceObjects)=='undefined' || !(faceObjects instanceof Array)){
            return
        }
        var faceIdx = faceOpt.faceIdx;
        if(null==faceIdx || typeof(faceIdx)!="number"){
            return
        }
        if(faceIdx<0 || faceIdx>5){
            return
        }
        var faceColor = faceOpt.faceColor;
        var faceFillOpts = faceOpt.fillOpts;
        if(null==faceFillOpts || typeof(faceFillOpts)=='undefined' || !(faceFillOpts instanceof Array)){
            return
        }
        var fillSize = faceFillOpts.length
        if(fillSize<1){
            return
        }
        var whOpt  = getCubeRangeFromPoints(points,faceIdx)
        if(null==whOpt || typeof(whOpt)=='undefined'){
            console.log('createFaceObjects 没有取到面['+faceIdx+']的长宽信息')
            return
        }
        var vWidth  = whOpt.width
        var vHeight = whOpt.height
        var faceObject = null;
        if(bPush){
            faceObject = new Object();
            faceObject.faceIdx = faceIdx;
            faceObject.unFinish = []
            faceObjects.push(faceObject)
            return
        }

        var iLoop
        var iLoopSize = faceObjects.length
        var tmp
        for(iLoop=0;iLoop<iLoopSize;iLoop++){
            tmp = faceObjects[iLoop]
            if(null!=tmp && typeof(tmp)!='undefined' && tmp.faceIdx == faceIdx){
                faceObject = tmp;
                break;
            }
        }
        if(null==faceObject || typeof(faceObject)=='undefined'){
            return
        }
        if(null==faceColor || typeof(faceColor)=='undefined' || faceColor==''){
            faceColor = "rgba(255,255,255,1)";
        }
        var ratio = 1;
        var hCanvas = document.createElement('canvas');
        hCanvas.width = vWidth * ratio;
        hCanvas.height = vHeight * ratio;
        hCanvas.style.width = vWidth + 'px';
        hCanvas.style.height = vHeight + 'px';
        var hContext = hCanvas.getContext('2d');
        hContext.clearRect(0, 0, vWidth, vHeight);
        hContext.fillStyle = faceColor;
        hContext.fillRect(0, 0, vWidth, vHeight);
      

        var fillIdx
        var faceFillOpt
        for(fillIdx=0;fillIdx<fillSize;fillIdx++){
            faceFillOpt = faceFillOpts[fillIdx]
            if(null==faceFillOpt || typeof(faceFillOpt)=='undefined'){
                continue
            }
            faceFillOpt.faceIdx = faceIdx
            faceFillOpt.fillIdx = fillIdx
            faceObject.unFinish.push(faceFillOpt)
        }
        for(fillIdx=0;fillIdx<fillSize;fillIdx++){
            faceFillOpt = faceFillOpts[fillIdx]
            if(null==faceFillOpt || typeof(faceFillOpt)=='undefined'){
                continue
            }
            var vColor = faceFillOpt.color;
            var vPic   = faceFillOpt.pic;
            var x      = faceFillOpt.x
            var y      = faceFillOpt.y
            var width  = faceFillOpt.width
            var height = faceFillOpt.height
            if(null==vColor || typeof(vColor)=='undefined' || ''==vColor){
                vColor = null
            }
            if(null==vPic || typeof(vPic)=='undefined' || ''==vPic){
                vPic = null
            }
            if(null==vPic && null==vColor){
                continue
            }
            
            if(null!=vColor){             
                // console.log('fillIdx=',fillIdx,'vColor=',vColor,'x=',x,'y=',y,'width=',width,'height=',height,'vWidth=',vWidth,'vHeight=',vHeight)   
                hContext.fillStyle = vColor;
                hContext.fillRect(x, y, width, height);
                afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,'')
            }else{
                //加载图片
                fillImg(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,hImgs)
            }
            
        }
    }
    function fillImg(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,hImgs) {
        var picUrl = faceFillOpt.pic;
        var x      = faceFillOpt.x
        var y      = faceFillOpt.y
        var width  = faceFillOpt.width
        var height = faceFillOpt.height

        var picUrl   = faceFillOpt.pic;
        var x      = faceFillOpt.x
        var y      = faceFillOpt.y
        var width  = faceFillOpt.width
        var height = faceFillOpt.height
        var vImage = hImgs[picUrl]
        if(null!=vImage && typeof(vImage)!='undefined'){
            hContext.drawImage(vImage, x, y, width, height);
            console.log('重复使用picUrl=',picUrl)
            afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,picUrl)
            return
        }
        // 创建一个新的Image对象
        var image = new Image();
        // 设置图片来源地址
        image.src = picUrl;
        // 为Image对象添加load事件监听
        image.onload = function() {
            // 当图片加载完成后，将其绘制到Canvas上
            // 参数分别为：图片的绘制位置x,y；要绘制的图片对象，目标矩形区域左上角x,y；目标矩形宽度，高度
            hContext.drawImage(image, x, y, width, height);
            if(null!=hImgs[picUrl]){
                hImgs[picUrl] = image
            }
            afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,picUrl)
        };

        // 如果需要处理加载错误，也可以添加error事件监听
        image.onerror = function() {
            console.error('图片加载失败');
            afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,picUrl)
        };
    }

    function fillImgOld(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,hImgs) {
        var picUrl   = faceFillOpt.pic;
        var x      = faceFillOpt.x
        var y      = faceFillOpt.y
        var width  = faceFillOpt.width
        var height = faceFillOpt.height
        try {
            var vImage = findImage(hImgs,picUrl)
            console.log('findImage=',picUrl,'vImage=',vImage)
            if(null!=vImage && typeof(vImage)!='undefined'){
                hContext.drawImage(vImage, x, y, width, height);
                console.log('重复使用picUrl=',picUrl)
                afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,picUrl)
                return
            }
            var loader = new THREE.ImageLoader();
            loader.load(picUrl,
                function (image) {
                    hContext.drawImage(image, x, y, width, height);
                    console.log('fillImg load =',picUrl,'image',image,hContext)

                    pushImage(hImgs,picUrl,image)
                    afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,picUrl)
                },
                undefined,
                function () {
                    var sUrl = picUrl
                    afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,picUrl)
                }
            );
        } catch (err) {
            console.log('fillImg',err);
        }
    }

    function afertAreateFace(faceIdx,faceObjects,faceObject,faceFillOpt,materials,hCanvas,hContext,picUrl){
        var bFaceObject = false;
        if(null!=faceObject && typeof(faceObject)!='undefined'){
            bFaceObject = true;
        }
        var iIdx;
        var iSize;
        var index = -1;
        var ratio = 1;
        var faceId1,faceId2,fillIdx1,fillIdx2
        var tmpObj;
        if(bFaceObject && null!=faceFillOpt && typeof(faceFillOpt)!='undefined'){
            var unFinish = faceObject.unFinish;
            if(null==unFinish || typeof(unFinish)=='undefined' || !(unFinish instanceof Array)){
                return
            }
            index = -1;
            iSize = unFinish.length
            faceId1 = faceFillOpt.faceIdx
            fillIdx1 = faceFillOpt.fillIdx
            for(iIdx=iSize-1;iIdx>=0;iIdx--){
                tmpObj = unFinish[iIdx]
                if(null==tmpObj || typeof(tmpObj)=='undefined'){
                    unFinish.splice(iIdx, 1); 
                    continue
                }
                faceId2 = tmpObj.faceIdx
                fillIdx2 = tmpObj.fillIdx
                if(faceId1==faceId2 && fillIdx2==fillIdx1){
                    unFinish.splice(iIdx, 1); 
                    continue
                }
            }
            iSize = unFinish.length
            // console.log('faceIdx=',faceIdx,'unFinish.size=',iSize)
            if(iSize>0){
                return
            }
            //没有未完成作业的面，生成材质
            if(null!=hContext && null!=hCanvas){
                if(null!=faceIdx && typeof(faceIdx)=="number" && faceIdx>=0 && faceIdx<6){
                    hContext.scale(ratio, ratio);
                    var hTexture = new THREE.Texture(hCanvas);
                    hTexture.needsUpdate = true;//, color: 0xff00ff, fog: true
                    materials[faceIdx] = new THREE.MeshPhongMaterial({ map: hTexture, side: THREE.DoubleSide });
                    //console.log('faceIdx=',faceIdx,'unFinish.size=',iSize,materials[faceIdx])
                }            
            }
        }
        //删除面信息
        if(null!=faceObjects && typeof(faceObjects)!='undefined' && faceObjects instanceof Array){
            iSize = faceObjects.length
            //删除为空的面、删除没有未完成作业的面
            for(iIdx=iSize-1;iIdx>=0;iIdx--){
                tmpObj = faceObjects[iIdx]
                if(null==tmpObj || typeof(tmpObj)=='undefined'){
                    faceObjects.splice(iIdx, 1); 
                    continue
                }
                if(null==tmpObj.unFinish || typeof(tmpObj.unFinish)=='undefined'){
                    faceObjects.splice(iIdx, 1); 
                    continue
                }
                if(tmpObj.unFinish.length<1){
                    faceObjects.splice(iIdx, 1); 
                    continue
                }
                
            }
            iSize = faceObjects.length
            if(iSize>0){
                return
            }
        }
        createCubeGeometry(points,materials,hCubes)
    }
    //创建长方体
    function createCubeGeometry(points,materials,hCubes,pColor  ){
        if(null==materials || typeof(faceObjects)=='undefined' || !(materials instanceof Array)){
            materials = [null,null,null,null,null,null]
        }
        //长方体配置的颜色
        if(null==pColor || typeof(pColor)=='undefined' || pColor==''){
            if(null!=cubeOpt && typeof(cubeOpt)!='undefined' && (cubeOpt instanceof Object)){
                var data = cubeOpt.data
                if(null!=data && typeof(data)!='undefined' && (data instanceof Object)){
                    pColor = data.color;
                }
            }
        }
        if(null==pColor || typeof(pColor)=='undefined' || pColor==''){
            pColor = 0xff00ff;
        }
        var material2 = null;
        //补充材质
        var iSize = materials.length
        var material;
        var index;
        for(index=0;index<iSize;index++){
            material = materials[index];
            if(null==material){
                if(null==material2){
                    material2 = new THREE.MeshPhongMaterial({ color: pColor, side: THREE.DoubleSide });
                }                
                materials[index] = material2;
            }
        }
        var data       = cubeOpt.data;
        if(null==data){
            data = {}
        }
        var desc       = data.desc
        var opt        = {}
        //创建长方形
        var points     = cubeOpt.points;
        if(null==points || typeof(points)=='undefined' || !(points instanceof Array)){
            console.log('创建长方体[',desc,']没有指定顶点')
            return
        }
        var iPointSize = points.length
        if(iPointSize!=4 && iPointSize<8){
            console.log('创建长方体[',desc,']顶点个数[',iPointSize,']不为4个或者8个')
            return
        }
        //将4个顶点转为8个顶点
        if(iPointSize==4){
            //由平面4个点转变为8个点
            points = pointsFrom4To8(points,data,opt,desc)
        }
        var hGeometry = new THREE.BufferGeometry();
        var vertices = getCubeFacesVertices(points,desc);
        hGeometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(vertices), 3));
        hGeometry.setIndex(indices);
        hGeometry.setAttribute('uv', new THREE.BufferAttribute(uvs, 2))
        hGeometry.computeVertexNormals()
        var hMesh = new THREE.Mesh(hGeometry, materials);
        hGeometry.groups = groups
        hMesh.userData = {data:data,points:points}
        if(hCubes instanceof Array){
            hCubes.push(hMesh)
        }
        // 存储原始位置信息
        initialPositions instanceof Map && initialPositions.set(hMesh,hMesh.position.clone())
        hScene.add(hMesh);
    }

    function getCubeType(data){
        var type = null;
        if(null==data || typeof(data)=='undefined'){
            type = CUBE_TYPE_DEF
        }else if (typeof(data)=='string'){
            type = data
        }else if (typeof(data)=='object'){
            type = data.CUBE_TYPE_DEF
        }
        if(null==type || typeof(type)=='undefined'){
            type = CUBE_TYPE_DEF
        }
        var sType = ','+type+','
        if(CUBE_TYPES.indexOf(sType)){
            type = CUBE_TYPE_DEF
        }
        return type
    }
    //判别是否为数字
    function isNUmber(num) {
        if(num == null || typeof(num)=='undefined'){
            return false
        }
        return /^[0-9]+.?[0-9]*$/.test(num);
    }

    //取得数字属性
    function getNum(obj,attr,def) {
        if(num == obj || typeof(obj)=='undefined'|| typeof(obj)!='object'){
            return def
        }
        if(num == attr || typeof(attr)=='undefined'|| typeof(attr)!='string' || attr==''){
            return def
        }
        var res = obj[attr]
        if(num == ret || typeof(ret)=='undefined' || ret==''){
            return def
        }
        var tmp = ''+res
        if(!isNUmber(tmp)){
            return def
        }
        return res;
    }
    //由平面4个点转变为8个点
    function pointsFrom4To8(points,data,opt,desc){
        //判别点的合法性
        if(points == null || typeof(points)=='undefined'){
            return null
        }
        if(desc == null || typeof(desc)=='undefined'){
            desc = ''
        }
        if(opt == null || typeof(opt)=='undefined'){
            opt = {}
        }
        

        var iSize = 0
        if(points instanceof Array){
            iSize = points.length
        }
        if(iSize!=4){
            return null
        }
        
        var cg  = getNum(opt,'cg',330) //层高
        var lg  = getNum(opt,'lg',30) //梁高
        var dbg = getNum(opt,'dbg',20) //地板高
        var type = getCubeType(data);
        var idx;
        var point
        var x,y,z,z1
        for(idx=0;idx<iSize;idx++){
            point = points[idx]
            if(point == null || typeof(point)=='undefined'){
                console.log(desc,'顶点坐标存在null值');
                return null
            }
            x = point.x
            y = point.y
            z = point.z
            if(!isNUmber(x)){
                console.log(desc,'第['+idx+']顶点X坐标不为数字');
                return null
            }
            if(!isNUmber(y)){
                console.log(desc,'第['+idx+']顶点Y坐标不为数字');
                return null
            }
            if(!isNUmber(z)){
                z = 0
            }
            if(type==CUBE_TYPE_Z){ //'Z'  //立柱
                z1 = z + cg 
                z  = z + 0
            }else if(type==CUBE_TYPE_L){ //'L'  //梁
                z1 = z + cg
                z  = z + cg-lg
            }else if(type==CUBE_TYPE_LQ){ //'LQ' 梁墙
                z1 = z + cg
                z  = z + 0
            }else if(type==CUBE_TYPE_DB){ //'DB' 地板
                z1 = z + cg+dbg
                z  = z + cg
            }
            point.z = z
            points.push({x:z,y:y,z:z1})
        }
        return points
    }

    // 将长方体的立方体的8个顶点转变为长方体的面的顶点
    function getCubeFacesVertices(points,desc) {
        if(null==points || typeof(points)=='undefined' || !(points instanceof Array)){
            return null
        }
        if(null==desc || typeof(desc)=='undefined' ){
            desc = ''
        }
        var iSize = points.length
        if(iSize<8){
            return null
        }
        var iIdx
        var point
        for(iIdx=0;iIdx<8;iIdx++){
            point = points[iIdx]
            if(null==point){
                console.log(desc,'长方体顶点存在有空值点')
                return null
            }
            //检查是否存在坐标非法点
        }
        // 每个面的顶点（顺时针或逆时针）
        const faces = [            
            [0, 1, 2, 3],// 底面            
            [4, 5, 6, 7],// 顶面            
            [0, 1, 5, 4],// 前面            
            [2, 3, 7, 6],// 后面            
            [0, 3, 7, 4],// 左面            
            [1, 2, 6, 5] // 右面
        ];
        // 生成顶点数组
        const facesVertices = [];
        faces.forEach(face => {
            face.forEach(index => {
                point = points[index]
                facesVertices.push(point.x);
                facesVertices.push(point.y);
                facesVertices.push(point.z);
            });
        });
        return facesVertices;
    }

    //将二维xy坐标转变为三维坐标数组
    function pointsToVertices4(points,type){
        var vertices = new Float32Array(24);
        return vertices
    }
    //将三维xyz坐标坐标转变为数组
    function pointsToVertices8(points){
        if(null==points || typeof(points)=='undefined' || !(points instanceof Array)){
            return null
        }
        var x,y,z
        var iSize = points.length;
        var vertices = new Float32Array(24);
        var iIdx,point
        var pIdx = 0
        for(iIdx=0;iIdx<iSize;iIdx++){
            point = points[iIdx];
            if(null==point || typeof(point)=='undefined'){
                continue
            }
            x = point.x
            y = point.y
            z = point.z
            if(z ==null || typeof(z)=='undefined'){
                z = 0
            }
            vertices[pIdx++]= x;
            vertices[pIdx++]= y;
            vertices[pIdx++]= z;
        }
        return vertices
    }
    //显示顶点坐标
    function showVerticesEx(vertices){
        if(vertices==null ||typeof(vertices)=='undefined'){
            return
        }
        var iSize = 0
        if(vertices instanceof Float32Array){
            iSize = vertices.length
        }
        var idx = 0
        var expr = ''
        var tmp = ''
        while(idx<iSize){
            expr += vertices[idx++] + ','
            if(idx>0 && idx%3==0){
                console.log(expr)
                expr = ''
            }
        }
        if(expr!=''){
            console.log(expr)
        }
        
    }
    //求长方体面的长宽
    function getCubeRangeFromPoints(points,faceIdx){
        if(null==points|| typeof(points)=='undefined'){
            return null
        }
        var iSize = 0
        if(points instanceof Array){
            iSize = points.length
        }
        if(iSize<8){
            return null
        }
        /*
            [0, 1, 2, 3],// 底面            
            [4, 5, 6, 7],// 顶面            
            [0, 1, 5, 4],// 前面            
            [2, 3, 7, 6],// 后面            
            [0, 3, 7, 4],// 左面            
            [1, 2, 6, 5] // 右面
        */
        var rWidth  = 0
        var rHeight = 0
        var p1,p2,p3,p4
        var iSize = points.length;
        var faceDesc=''
        switch (faceIdx) {
            case 0: //[0, 1, 2, 3],// 底面
                p1 = points[0]
                p2 = points[1]
                p3 = points[2]
                p4 = points[3]
                faceDesc='底面[0, 1, 2, 3]'
                rWidth  = Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y,2)+Math.pow(p2.z-p1.z,2))
                rHeight = Math.sqrt(Math.pow(p4.x-p1.x,2)+Math.pow(p4.y-p1.y,2)+Math.pow(p4.z-p1.z,2))
                break;
            case 1: //[4, 5, 6, 7],// 顶面 
                p1 = points[4]
                p2 = points[5]
                p3 = points[6]
                p4 = points[7]
                faceDesc='顶面[4, 5, 6, 7]'
                rWidth  = Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y,2)+Math.pow(p2.z-p1.z,2))
                rHeight = Math.sqrt(Math.pow(p4.x-p1.x,2)+Math.pow(p4.y-p1.y,2)+Math.pow(p4.z-p1.z,2))
                break;
            case 2://[0, 1, 5, 4],// 前面 
                p1 = points[0]
                p2 = points[1]
                p3 = points[5]
                p4 = points[4]
                faceDesc='前面[0, 1, 5, 4]'
                rWidth  = Math.sqrt(Math.pow(p4.x-p3.x,2)+Math.pow(p4.y-p3.y,2)+Math.pow(p4.z-p3.z,2))
                rHeight = Math.sqrt(Math.pow(p4.x-p1.x,2)+Math.pow(p4.y-p1.y,2)+Math.pow(p4.z-p1.z,2))
                break;
            case 3: //[2, 3, 7, 6],// 后面    
                p1 = points[2]
                p2 = points[3]
                p3 = points[7]
                p4 = points[6]
                faceDesc='后面[2, 3, 7, 6]'
                rWidth  = Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y,2)+Math.pow(p2.z-p1.z,2))
                rHeight = Math.sqrt(Math.pow(p2.x-p3.x,2)+Math.pow(p2.y-p3.y,2)+Math.pow(p2.z-p3.z,2))
                break;
            case 4: //[0, 3, 7, 4],// 左面
                p1 = points[0]
                p2 = points[3]
                p3 = points[7]
                p4 = points[4]
                faceDesc='左面[0, 3, 7, 4]'
                rWidth  = Math.sqrt(Math.pow(p4.x-p1.x,2)+Math.pow(p4.y-p1.y,2)+Math.pow(p4.z-p1.z,2))
                rHeight = Math.sqrt(Math.pow(p4.x-p3.x,2)+Math.pow(p4.y-p3.y,2)+Math.pow(p4.z-p3.z,2))
                break;
            case 5://[1, 2, 6, 5] // 右面
                p1 = points[1]
                p2 = points[2]
                p3 = points[6]
                p4 = points[5]
                faceDesc='右面[1, 2, 6, 5]'
                rWidth  = Math.sqrt(Math.pow(p4.x-p1.x,2)+Math.pow(p4.y-p1.y,2)+Math.pow(p4.z-p1.z,2))
                rHeight = Math.sqrt(Math.pow(p2.x-p1.x,2)+Math.pow(p2.y-p1.y,2)+Math.pow(p2.z-p1.z,2))
                break;
        }
        // console.log('faceIdx=',faceIdx,'faceDesc=',faceDesc,'iSize=',iSize,'rWidth=',rWidth,'rHeight=',rHeight,'(',p1.x,',',p1.y,',',p1.z,'),','(',p2.x,',',p2.y,',',p2.z,'),','(',p3.x,',',p3.y,',',p3.z,'),','(',p4.x,',',p4.y,',',p4.z,'),');
        return {width:rWidth,height:rHeight};
    }


    //求长方体面的长宽
    function getCubeRange(vertices,faceIdx){
        if(null==vertices|| typeof(vertices)=='undefined'){
            return null
        }
        var iSize = 0
        if(vertices instanceof Float32Array){
            iSize = vertices.length
        }
        if(iSize!=24){
            return null
        }
        var pArr = vertices;
        /*
        0, 1, 2, 0, 2, 3, // 底面
        4, 6, 5, 4, 7, 6, // 顶面
        0, 4, 5, 0, 5, 1, // 右面
        1, 5, 6, 1, 6, 2, // 前面
        2, 6, 7, 2, 7, 3, // 左面
        3, 7, 4, 3, 4, 0, // 背面
        */
        var x1,y1,z1
        var x2,y2,z2
        var x3,y3,z3
        var x4,y4,z4
        var rWidth  = 0
        var rHeight = 0
        
        var iSize = positions.array.length;
        var faceDesc=''
        switch (faceIdx) {
            case 0: //0, 1, 2, 0, 2, 3, // 底面
                x1 = pArr[0]
                y1 = pArr[1]
                z1 = pArr[2]
                x2 = pArr[3]
                y2 = pArr[4]
                z2 = pArr[5]
                x3 = pArr[6]
                y3 = pArr[7]
                z3 = pArr[8]
                x4 = pArr[9]
                y4 = pArr[10]
                z4 = pArr[11]
                faceDesc='底面 0, 1, 2'
                rWidth  = Math.sqrt(Math.pow(x4-x1,2)+Math.pow(y4-y1,2)+Math.pow(z4-z1,2))
                rHeight = Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)+Math.pow(z2-z3,2))
                break;
            case 1: //4, 6, 5, 4, 7, 6, // 顶面
                x1 = pArr[12]
                y1 = pArr[13]
                z1 = pArr[14]
                x2 = pArr[18]
                y2 = pArr[19]
                z2 = pArr[20]
                x3 = pArr[15]
                y3 = pArr[16]
                z3 = pArr[17]
                x4 = pArr[21]
                y4 = pArr[22]
                z4 = pArr[23]
                faceDesc='顶面 4, 6, 5'
                rWidth  = Math.sqrt(Math.pow(x4-x1,2)+Math.pow(y4-y1,2)+Math.pow(z4-z1,2))
                rHeight = Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)+Math.pow(z2-z3,2))
                break;
            case 2://0, 4, 5, 0, 5, 1, // 前面
                x1 = pArr[0]
                y1 = pArr[1]
                z1 = pArr[2]
                x2 = pArr[12]
                y2 = pArr[13]
                z2 = pArr[14]
                x3 = pArr[15]
                y3 = pArr[16]
                z3 = pArr[17]
                x4 = pArr[3]
                y4 = pArr[4]
                z4 = pArr[5]
                faceDesc='前面 0, 4, 5'
                rWidth  = Math.sqrt(Math.pow(x4-x1,2)+Math.pow(y4-y1,2)+Math.pow(z4-z1,2))
                rHeight = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)+Math.pow(z4-z3,2))
                break;
            case 3: //1, 5, 6, 1, 6, 2, // 右面
                x1 = pArr[3]
                y1 = pArr[4]
                z1 = pArr[5]
                x2 = pArr[15]
                y2 = pArr[16]
                z2 = pArr[17]
                x3 = pArr[18]
                y3 = pArr[19]
                z3 = pArr[20]
                x4 = pArr[6]
                y4 = pArr[7]
                z4 = pArr[8]
                faceDesc='右面 1, 5, 6'
                rWidth  = Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)+Math.pow(z2-z3,2))
                rHeight = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)+Math.pow(z2-z1,2))
                break;
            case 4: //2, 6, 7, 2, 7, 3, // 背面
                x1 = pArr[3]
                y1 = pArr[4]
                z1 = pArr[5]
                x2 = pArr[18]
                y2 = pArr[19]
                z2 = pArr[20]
                x3 = pArr[21]
                y3 = pArr[22]
                z3 = pArr[23]
                x4 = pArr[9]
                y4 = pArr[10]
                z4 = pArr[11]
                faceDesc='背面 2, 6, 7'
                rWidth  = Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)+Math.pow(z2-z3,2))
                rHeight = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)+Math.pow(z4-z3,2))
                break;
            case 5://3, 7, 4, 3, 4, 0, //左面
                x1 = pArr[9]
                y1 = pArr[10]
                z1 = pArr[11]
                x2 = pArr[21]
                y2 = pArr[22]
                z2 = pArr[23]
                x3 = pArr[12]
                y3 = pArr[13]
                z3 = pArr[14]
                x4 = pArr[0]
                y4 = pArr[1]
                z4 = pArr[2]
                faceDesc='左面 3, 7, 4'
                rWidth  = Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)+Math.pow(z2-z3,2))
                rHeight = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)+Math.pow(z4-z3,2))
                break;
        }
        // console.log('faceIdx=',faceIdx,'faceDesc=',faceDesc,'iSize=',iSize,'rWidth=',rWidth,'rHeight=',rHeight,'(',x1,',',y1,',',z1,'),','(',x2,',',y2,',',z2,'),','(',x3,',',y3,',',z3,'),','(',x4,',',y4,',',z4,'),');
        return {width:rWidth,height:rHeight};
    }
}